package com.marktotop.controller.system;


import com.marktotop.annotation.LogAnnotation;
import com.marktotop.converter.RoleConverter;
import com.marktotop.entity.system.Role;
import com.marktotop.error.SystemException;
import com.marktotop.response.ResultBean;
import com.marktotop.response.ResultCode;
import com.marktotop.service.IMenuService;
import com.marktotop.service.IRoleService;
import com.marktotop.service.IUserService;
import com.marktotop.vo.system.MenuNodeVO;
import com.marktotop.vo.system.RoleTransferItemVO;
import com.marktotop.vo.system.RoleVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 角色表 前端控制器
 * </p>
 *
 * @author MarkToTop
 * @since 2021-07-12
 */
@RestController
@RequestMapping("/system/roles")
@Api(tags = "系统模块-角色相关接口")
public class RoleController {

    @Autowired
    private IUserService userService;

    @Autowired
    private IRoleService roleService;

    @Autowired
    private IMenuService menuService;

    /**
     * 用户角色信息
     * @param id
     * @return
     */
//    @LogAnnotation(module = "角色模块",operator = "查询已有角色模块")
    @ApiOperation(value = "已有角色", notes = "根据用户id，获取用户已经拥有的角色")
    @RequestMapping(value = "/{id}/roles",method = RequestMethod.GET)
    public ResultBean roles(@PathVariable Long id) throws SystemException {
        List<Long> values = userService.roles(id);
        List<Role> list = roleService.list();
        // 转成前端需要的角色Item
        List<RoleTransferItemVO> items = RoleConverter.converterToRoleTransferItem(list);
        Map<String, Object> map = new HashMap<>();
        map.put("roles", items);
        map.put("values", values);
        return ResultBean.ok().data("idRoles",map);
    }


    /**
     * 角色列表
     *
     * @return
     */
    @ApiOperation(value = "角色列表")
    @GetMapping("/findRoleList")
    public ResultBean findRoleList(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                   @RequestParam(value = "pageSize", defaultValue = "8") Integer pageSize,
                                   RoleVO roleVO) {
        return roleService.findRoleList(pageNum, pageSize, roleVO);
    }


    /**
     * 角色授权
     *
     * @param mids
     * @return
     */
    @ApiOperation(value = "角色授权",notes = "给角色分配相应的权限")
    @RequiresPermissions({"role:authority"})
    @RequestMapping(value = "/authority/{id}",method = RequestMethod.POST)
    public ResultBean authority(@PathVariable Long id, @RequestBody Long[] mids) throws SystemException {
        roleService.authority(id, mids);
        return ResultBean.ok().message("角色授权操作成功！");
    }



    /**
     * 添加角色信息
     *
     * @param roleVO
     * @return
     */
    @ApiOperation(value = "添加角色",notes = "添加新的角色信息")
    @RequiresPermissions({"role:add"})
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public ResultBean add(@RequestBody @Validated RoleVO roleVO) throws SystemException {
        roleService.saveRole(roleVO);
        return ResultBean.ok().message("添加新的角色信息成功!");
    }


    /**
     * 删除角色
     *
     * @param id 角色ID
     * @return
     */
    @ApiOperation(value = "删除角色", notes = "根据id删除角色信息")
    @RequiresPermissions({"role:delete"})
    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    public ResultBean delete(@PathVariable Long id) throws SystemException {
        if (!roleService.removeById(id)) {
            return ResultBean.error().message("删除角色信息失败！");
        }
        return ResultBean.ok().message("删除角色信息成功！");
    }


    /**
     * 编辑角色信息
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "编辑用户", notes = "根据id更新角色信息")
    @RequestMapping(value = "/edit/{id}",method = RequestMethod.GET)
    @RequiresPermissions({"role:edit"})
    public ResultBean edit(@PathVariable Long id) throws SystemException {
        RoleVO roleVO = roleService.editRole(id);
        return ResultBean.ok().data("role",roleVO);
    }


    /**
     * 角色拥有的菜单权限id和菜单树
     *
     * @param id
     * @return
     */
    @ApiOperation(value = "角色菜单",notes = "角色拥有的菜单权限id和菜单树")
    @RequestMapping(value = "/findRoleMenu/{id}",method = RequestMethod.GET)
    public ResultBean findRoleMenu(@PathVariable Long id) throws SystemException {
        List<MenuNodeVO> tree = menuService.findMenuTree();
        //角色拥有的菜单id
        List<Long> mids = roleService.findMenuIdsByRoleId(id);
        List<Long> ids = menuService.findOpenIds();
        Map<String, Object> map = new HashMap<>();
        map.put("tree", tree);
        map.put("mids", mids);
        map.put("open", ids);
        return ResultBean.ok().data("roleMenu",map).message("获取角色拥有的菜单权限和菜单树成功!");
    }


    /**
     * 更新角色
     *
     * @param id
     * @param roleVO
     * @return
     */
    @ApiOperation(value = "更新角色", notes = "根据id更新角色信息")
    @RequiresPermissions({"role:update"})
    @RequestMapping(value = "/update/{id}",method = RequestMethod.PUT)
    public ResultBean update(@PathVariable Long id, @RequestBody @Validated RoleVO roleVO) throws SystemException {
        roleService.updateRole(id, roleVO);
        return ResultBean.ok().message("更新角色信息操作成功!");
    }

    /**
     * 更新角色状态
     *
     * @param id
     * @param status
     * @return
     */
    @ApiOperation(value = "更新状态", notes = "禁用和更新两种状态")
    @RequiresPermissions({"role:status"})
    @RequestMapping(value = "/updateStatus/{id}/{status}",method = RequestMethod.PUT)
    public ResultBean updateStatus(@PathVariable Long id, @PathVariable Boolean status) throws SystemException {
        roleService.updateStatus(id, status);
        return ResultBean.ok().message("更新角色状态成功");
    }
}
